package view;

import entity.ChessUser;
import message.LoginMessage;
import socket.ChessClient;
import socket.ChessServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 新注册的用户选择头像界面.
 *
 * @author 张天羿
 */

public class UpdatePicture extends JFrame {
    private ChessServer chessServer = ChessServer.getInstance();
    private ChessUser chessUser;
    private JLabel iconButton1;
    private JLabel iconButton2;
    private JLabel iconButton3;
    private JLabel iconButton4;
    private JLabel iconButton5;
    private JLabel iconButton6;
    private JLabel iconButton7;
    private JLabel iconButton8;
    private JLabel iconButton9;
    private JLabel iconButton10;
    private JLabel iconButton11;
    private JLabel iconButton12;
    private JLabel iconButton13;
    private JLabel iconButton14;
    private JLabel iconButton15;
    private JLabel iconButton16;
    private JLabel iconButton17;
    private JLabel iconButton18;
    private JLabel iconButton19;

    public UpdatePicture(ChessUser chessUser) {

        this.chessUser = chessUser;
        chessServer.getChessUserService().insertUser(chessUser);
        this.setBounds(100, 100, 400, 400);
        this.setResizable(false);
        setVisible(true);
        getContentPane().setLayout(new GridLayout(5, 10));

        initButton();
        addAction();
    }

    private void initButton() {
        iconButton1 = new JLabel(new ImageIcon("resource/photo/1.jpg"));
        getContentPane().add(iconButton1);
        iconButton2 = new JLabel(new ImageIcon("resource/photo/2.jpg"));
        getContentPane().add(iconButton2);
        iconButton3 = new JLabel(new ImageIcon("resource/photo/3.jpg"));
        getContentPane().add(iconButton3);
        iconButton4 = new JLabel(new ImageIcon("resource/photo/4.jpg"));
        getContentPane().add(iconButton4);
        iconButton5 = new JLabel(new ImageIcon("resource/photo/5.jpg"));
        getContentPane().add(iconButton5);
        iconButton6 = new JLabel(new ImageIcon("resource/photo/6.jpg"));
        getContentPane().add(iconButton6);
        iconButton7 = new JLabel(new ImageIcon("resource/photo/7.jpg"));
        getContentPane().add(iconButton7);
        iconButton8 = new JLabel(new ImageIcon("resource/photo/8.jpg"));
        getContentPane().add(iconButton8);
        iconButton9 = new JLabel(new ImageIcon("resource/photo/9.jpg"));
        getContentPane().add(iconButton9);
        iconButton10 = new JLabel(new ImageIcon("resource/photo/10.jpg"));
        getContentPane().add(iconButton10);
        iconButton11 = new JLabel(new ImageIcon("resource/photo/11.jpg"));
        getContentPane().add(iconButton11);
        iconButton12 = new JLabel(new ImageIcon("resource/photo/12.jpg"));
        getContentPane().add(iconButton12);
        iconButton13 = new JLabel(new ImageIcon("resource/photo/13.jpg"));
        getContentPane().add(iconButton13);
        iconButton14 = new JLabel(new ImageIcon("resource/photo/14.jpg"));
        getContentPane().add(iconButton14);
        iconButton15 = new JLabel(new ImageIcon("resource/photo/15.jpg"));
        getContentPane().add(iconButton15);
        iconButton16 = new JLabel(new ImageIcon("resource/photo/16.jpg"));
        getContentPane().add(iconButton16);
        iconButton17 = new JLabel(new ImageIcon("resource/photo/17.jpg"));
        getContentPane().add(iconButton17);
        iconButton18 = new JLabel(new ImageIcon("resource/photo/18.jpg"));
        getContentPane().add(iconButton18);
        iconButton19 = new JLabel(new ImageIcon("resource/photo/19.jpg"));
        getContentPane().add(iconButton19);
    }


    private void addAction() {

        iconButton1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/1.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }

        });

        iconButton2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/2.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/3.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/4.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });
        iconButton5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/5.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/6.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton7.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/7.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton8.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/8.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton9.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/9.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton10.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/10.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton11.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/11.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton12.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/12.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton13.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/13.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton14.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/14.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton15.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/15.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton16.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/16.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton17.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/17.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton18.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/18.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

        iconButton19.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String root1 = "resource/photo/19.jpg";
                chessServer.getChessUserService().updateUserImg(root1, chessUser.getName());
                chessUser.setFileName(root1);
                login();
            }
        });

    }

    /**
     * 选择之后默认进行登录操作.
     */
    public void login() {
        dispose();
        //发送登录报文
        LoginMessage loginMessage = new LoginMessage(chessUser.getName());
        ChessClient.getInstance().sendMessage(loginMessage);
    }

}


